# 单例模式实现方式之-
"""实现__new__方法，然后将类的一个实例绑定到类变量_instance上；如果cls._instance为None，则说明该类还没有被实例化过，
new一个该类的实例，并返回；如果cls._instance不为None，直接返回_instance，"""
class Singleton(object):
    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, '_instance'):
            orig = super(Singleton, cls)
            cls._instance = orig.__new__(cls, *args, **kwargs)
        return cls._instance
class MyClass(Singleton):
    a = 1

one = MyClass()
two = MyClass()
print(one.a)
print(id(one))
print(id(two))
print(one == two)
print(one is two)